package com.hzt.agprosaleplatform.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSException;
import lombok.extern.slf4j.Slf4j;
import com.aliyun.oss.OSS;
import lombok.NonNull;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Component
public class OssUtil {
    @Value("${oss.bucket}")
    private String OSS_BUCKET;

    @Value("${oss.bucketUrl}")
    private String OSS_BUCKET_URL;

    @Autowired
    private OSS ossClient;

    //上传照片
    public String uploadImage(@NonNull MultipartFile file, @NonNull String type) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;   // 完整文件路径名

        String filePathName;
        if (type.equals("product-img")) {
            // 主图使用固定的日期路径
            filePathName = "20241102/product-img/" + fileName;
        } else {
            // 其他图片使用当前日期
            String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
            filePathName = currentDate + "/img" + type + "/" + fileName;
        }

        try {
            ossClient.putObject(
                    OSS_BUCKET,
                    filePathName,
                    file.getInputStream() // 数据流
            );
        } catch (OSSException oe) {
            log.error("OSS出错了:" + oe.getErrorMessage());
        } catch (ClientException ce) {
            log.error("OSS连接出错了" + ce.getMessage());
        }
        return OSS_BUCKET_URL + filePathName;
    }

    /**
     * 上传文章照片功能
     * @param file
     * @param type
     * @return
     * @throws IOException
     */
    public String uploadArticleImg(@NonNull MultipartFile file, @NonNull String type) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;

        String filePathName = null;
        String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());

        if(type.equals("coverImg")){
            filePathName = currentDate + "/coverImg/" + fileName;
        }
        if(type.equals("contentImg")) {
            filePathName = currentDate + "/contentImg/" + fileName;
        }

        try{
            ossClient.putObject(
                    OSS_BUCKET,
                    filePathName,
                    file.getInputStream()
            );
        }catch (OSSException oe) {
            log.error("OSS出错了:" + oe.getErrorMessage());
        } catch (ClientException ce) {
            log.error("OSS连接出错了" + ce.getMessage());
        }
        return OSS_BUCKET_URL + filePathName;
    }


    /**
     * OSS删除文件
     * @param filename
     */
    public void deleteFiles(@NonNull String filename) {
        try {
            if (filename.isEmpty()) {
                log.warn("文件名为空，拒绝删除操作");
                return;
            }
            
            ossClient.deleteObject(OSS_BUCKET, filename);
            log.info("成功删除文件: " + filename);
        } catch (OSSException oe) {
            log.error("删除OSS文件出错: " + oe.getErrorMessage());
        } catch (ClientException ce) {
            log.error("OSS连接出错: " + ce.getMessage());
        }
    }

}
